/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.language.OnEnglishDateBuildAdapter;
import com.zhuangfei.ohos_timetableview.language.OnEnglishItemBuildAdapter;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnItemBuildAdapter;
import com.zhuangfei.timetable.model.Schedule;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

import java.util.List;

/**
 * ItemStyleSlice
 *
 * @since 2021-03-29
 * */
public class ItemStyleSlice extends BaseAbilitySlice {
    private static final String[] SPIN_MUNE = {"切换英文", "切换中文", "隐藏非本周课程", "显示非本周课程",
            "边距与弧度设置", "课程项文本", "单个角弧度设置", "非本周背景设置", "修改课程重叠的样式"};

    TimetableView mTimetableView;
    MaterialSpinner moreButton;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_custom_item_style);
        initViews();
        intents = intent;
    }

    private void initViews() {
        moreButton = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        moreButton.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                selectLayout(position);
                moreButton.setShowFalse();

            }
        });
        moreButton.setItems(SPIN_MUNE);
        moreButton.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {

            @Override
            public void onNothingSelected(MaterialSpinner spinner) {
                spinner.setShowFalse();
            }
        });

        initTimetableView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mySubjects.add(mySubjects.get(0));
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .showView();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        MaterialSpinner spinner = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                selectLayout(position);
                spinner.setShowFalse();
            }
        });
        spinner.setItems(SPIN_MUNE);
        spinner.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {

            @Override
            public void onNothingSelected(MaterialSpinner spinner) {
                spinner.setShowFalse();
            }
        });
    }

    private void selectLayout(int position) {
        switch (position) {
            case 0:
                changeEnglishLanguage();
                break;
            case 1:
                changeChineseLanguage();
                break;
            case 2:
                hideNonThisWeek();
                break;
            case 3:
                showNonThisWeek();
                break;
            case 4:
                setMarginAndCorner();
                break;
            case 5:
                buildItemText();
                break;
            case 6:
                setCorner(0, 10, 0, 0);
                break;
            case 7:
                setNonThisWeekBgcolor(new RgbColor(255, 255, 0));
                break;
            case 8:
                modifyOverlayStyle();
                break;
            default:
                break;
        }
    }

    /**
     * 隐藏非本周课程
     * 修改了内容的显示，所以必须更新全部（性能不高）
     * 建议：在初始化时设置该属性
     */
    protected void hideNonThisWeek() {
        mTimetableView.isShowNotCurWeek(false).updateView();
    }

    /**
     * 显示非本周课程
     * 修改了内容的显示，所以必须更新全部（性能不高）
     * 建议：在初始化时设置该属性
     */
    protected void showNonThisWeek() {
        mTimetableView.isShowNotCurWeek(true).updateView();
    }

    /**
     * 设置间距以及弧度
     * 该方法只能同时设置四个角的弧度，设置单个角的弧度可参考下文
     */
    protected void setMarginAndCorner() {
        mTimetableView.cornerAll(0)
                .marTop(0)
                .marLeft(0)
                .updateView();
    }

    /**
     * 设置角度（四个角分别控制）
     *
     * @param leftTop
     * @param rightTop
     * @param rightBottom
     * @param leftBottom
     */
    public void setCorner(final int leftTop, final int rightTop, final int rightBottom, final int leftBottom) {
        mTimetableView.callback(new OnItemBuildAdapter() {
            @Override
            public void onItemUpdate(StackLayout layout, Text textView,
                                     Text countTextView, Schedule schedule, ShapeElement gd) {
                super.onItemUpdate(layout, textView, countTextView, schedule, gd);
                /**
                 * 数组8个元素，四个方向依次为左上、右上、右下、左下，
                 * 每个方向在数组中占两个元素，值相同
                 */
                gd.setCornerRadiiArray(new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom,
                        rightBottom, leftBottom, leftBottom});
            }
        });
        mTimetableView.updateView();
    }

    /**
     * 修改显示的文本
     */
    public void buildItemText() {
        mTimetableView.callback(new OnItemBuildAdapter() {
            @Override
            public String getItemText(Schedule schedule, boolean isThisWeek) {
                if (isThisWeek) {
                    return "[本周]" + schedule.getName();
                } else {
                    return "[非本周]" + schedule.getName();
                }
            }
        })
                .updateView();
    }

    /**
     * 设置非本周课的背景
     *
     * @param color
     */
    public void setNonThisWeekBgcolor(RgbColor color) {
        mTimetableView.colorPool().setUselessColor(color);
        mTimetableView.updateView();
    }

    /**
     * 修改课程重叠的样式，在该接口中，你可以自定义出很多的效果
     */
    protected void modifyOverlayStyle() {
        mTimetableView.callback(new OnItemBuildAdapter() {
            @Override
            public void onItemUpdate(StackLayout layout, Text textView,
                                     Text countTextView, Schedule schedule, ShapeElement gd) {
                super.onItemUpdate(layout, textView, countTextView, schedule, gd);
                /**
                 * 可见说明重叠，取消角标，添加角度
                 */
                if (countTextView.getVisibility() == Component.VISIBLE) {
                    countTextView.setVisibility(Component.HIDE);
                    gd.setCornerRadiiArray(new float[]{0, 0, 20, 20, 0, 0, 0, 0});
                }
            }
        });
        mTimetableView.updateView();
    }

    /**
     * 切换为英文
     */
    public void changeEnglishLanguage() {
        mTimetableView.callback(new OnEnglishDateBuildAdapter())
                .callback(new OnEnglishItemBuildAdapter())
                .updateView();
    }

    /**
     * 切换为中文
     */
    public void changeChineseLanguage() {
        mTimetableView.callback((ISchedule.OnDateBuildListener) null)
                .callback((ISchedule.OnItemBuildListener) null)
                .updateView();
    }
}
